/*
 * Project: BoboDesignPattern
 * 
 * File Created at 2019年12月29日
 * 
 * Copyright 2012 Greenline.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Greenline Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Greenline.com.
 */
package test.practice;

import java.util.Arrays;

/**
 * @Type A20191229__fastsort
 * @Desc
 * @author 52489
 * @date 2019年12月29日
 * @Version V1.0
 */
public class A20191229_30_fastsort {

    public static void main(String[] args) {
        int nums[] = { 3, 3, 3, 3, 3, 3, 1, 4 };
        fastsort1(0, nums.length - 1, nums);
    }

    /**
     * @param i
     * @param length
     * @param nums
     */
    private static void fastsort(int startIdx, int endIdx, int[] nums) {
        if (startIdx < endIdx) {
            int tmp = nums[startIdx];
            int begin = startIdx;
            int end = endIdx;
            while (begin < end) {
                while (nums[end] > tmp && end > begin) {
                    end--;
                }
                while (nums[begin] <= tmp && end > begin) {
                    begin++;
                }
                int c = nums[end];
                nums[end] = nums[begin];
                nums[begin] = c;
            }
            nums[startIdx] = nums[begin];
            nums[begin] = tmp;
            fastsort(startIdx, begin - 1, nums);
            fastsort(begin + 1, endIdx, nums);
        }
        System.out.println(Arrays.toString(nums));
    }

    private static void fastsort1(int startIdx, int endIdx, int[] nums) {
        if (startIdx < endIdx) {
            int left = startIdx;
            int right = endIdx;
            int tmp = nums[left];
            while (left < right) {
                while (nums[right] > tmp && left < right) {
                    right--;
                }
                nums[left] = nums[right];
                while (nums[left] <= tmp && left < right) {
                    left++;
                }
                nums[right] = nums[left];
            }
            nums[left] = tmp;
            fastsort1(startIdx, left - 1, nums);
            fastsort1(left + 1, endIdx, nums);
        }

        System.out.println(Arrays.toString(nums));

    }

}
